package com.foreknow.demo13;

import java.util.Scanner;

public class Test {
    /**
     * s输出图书的信息
     */
    public void printBooksInfo(Book[] books){
        for (int i =0;i<books.length;i++){
            Book books1 =books[i];
            System.out.println(books1.getId()+"_____"+books1.getName()+"_____"+books1.getPrice()+"_____-"+books1.getStorage());
        }
    }

    /**
     * 购买图书
     */
    public Order buyBooks(Book[] books){
        Scanner input = new Scanner(System.in);
        //创建一个订单对象
        Order order = new Order("0001");
        //循环三次
        for (int i=0;i<3;i++){
            System.out.println("请输入图书的编号:");
            int pid =input.nextInt();
            System.out.println("请输入购买数量");
            int pnum = input.nextInt();
            //将接收到的数量保存到OrderItem
            OrderItem orderItem = new OrderItem(books[pid-1].getName(),books[pid-1].getPrice(),pnum);
            //将OrderItem保存到数组当中
            order.setItems(orderItem,i);
        }
        return order;
    }

    /**
     * 输出订单的信息
     */
    public void printOrderInfo(Order order){
//输出订单信息
        System.out.println("订单的编号: "+order.getOrderId());
        System.out.println("订单总额:"+ order.getTotal());
        //获取到Order对象中的数组进行遍历
        OrderItem[] items= order.getItems();
        for (int i= 0;i<items.length;i++){
            OrderItem orderItems = items[i];
            System.out.println(orderItems.getPname()+"------"+orderItems.getPprice()+"----"+orderItems.getNum());
        }
    }
    public static void main(String[] args) {
        Test test = new Test();
        //创建数组容器用于保存图书对象
        Book[] books = new Book[3];
        //创建三个图书对象
        Book book1 = new Book(1,"java",33.45,50);
        Book book2 = new Book(2,"spring",78.6,150);
        Book book3 = new Book(3,"CSS",45.3,250);

        books[0]=book1;
        books[1]=book2;
        books[2]=book3;

        //调用方法来输出图书的信息
        test.printBooksInfo(books);

        //调用购买图书的方法
        Order order =test.buyBooks(books);
////        System.out.println(order);

        test.printOrderInfo(order);

    }
}
